POWER.Name = "Levitation";
POWER.Desc = "Lets you fly around the map";

POWER.Version = 1;
POWER.Author = "Brian Nevec";

function POWER:Initialize( )
	
	hook.Add( "EntityTakeDamage", "lev.EntityTakeDamage", function( ... ) self:EntityTakeDamage( ... ); end );
	
end

//courtesy of conna
function POWER:Enabled( )
	
	local smoke = ents.Create( "env_smoketrail" );
	
	smoke:SetKeyValue( "opacity", 1 );
	smoke:SetKeyValue( "spawnrate", 80 );
	smoke:SetKeyValue( "lifetime", 5 );
	smoke:SetKeyValue( "startcolor", "240 240 240" );
	smoke:SetKeyValue( "endcolor", "220 220 220" );
	smoke:SetKeyValue( "minspeed", 5 );
	smoke:SetKeyValue( "maxspeed", 15 );
	smoke:SetKeyValue( "startsize", self.Owner:BoundingRadius( ) / 2 );
	smoke:SetKeyValue( "endsize", self.Owner:BoundingRadius( ) );
	smoke:SetKeyValue( "spawnradius", 10 );
	smoke:SetKeyValue( "emittime", 300 );
	smoke:SetKeyValue( "firesprite", "sprites/firetrail.spr" );
	smoke:SetKeyValue( "smokesprite", "sprites/whitepuff.spr" );
	smoke:SetKeyValue( "maxdirectedspeed", 0 );
	smoke:SetKeyValue( "mindirectedspeed", 0 );
	
	smoke:SetPos( self.Owner:GetPos( ) );
	smoke:SetParent( self.Owner );
	
	smoke:Spawn( );
	smoke:Activate( );
	
	self.Owner.SmokeTrail = smoke;
	
end

function POWER:Disabled( )

	if ValidEntity( self.Owner.SmokeTrail ) then
	
		self.Owner.SmokeTrail:Remove( );
	
	end

end

function POWER:Think( )
		
	local vel = self.Owner:GetVelocity( ):GetNormal( );
	local dir;
	
	if self.Owner:KeyDown( IN_FORWARD ) then
		
		dir = self.Owner:GetAimVector( ) + Vector( 0, 0, 0.4 );
		vel = vel + dir * 40;
		
	end
	
	if self.Owner:KeyDown( IN_BACK ) then
		
		dir = self.Owner:GetAimVector( );
		vel = vel + dir * -32;
		
	end
	
	if self.Owner:KeyDown( IN_MOVELEFT ) then
		
		dir = self.Owner:GetRight( );
		vel = vel + dir * -26;
		
	end
	
	if self.Owner:KeyDown( IN_MOVERIGHT ) then
		
		dir = self.Owner:GetRight( );
		vel = vel + dir * 26;
		
	end
	
	if self.Owner:KeyDown( IN_JUMP ) then
		
		dir =  Vector( 0, 0, 1 );
		vel = vel + dir * 50;
		
	end
	
	vel.x = math.Clamp( vel.x, -2000, 2000 );
	vel.y = math.Clamp( vel.y, -2000, 2000 );
	vel.z = math.Clamp( vel.z, -2000, 2000 );
	
	self.Owner:SetVelocity( vel );
	
end

function POWER:EntityTakeDamage( ent, wep, attacker, amount, info )

	if ent:IsPlayer( ) && ent.PowerId == self.Id && attacker:IsWorld( ) then
	
		info:ScaleDamage( 0 );
	
	end

end